import { createI18n } from 'vue-i18n'

function loadLanguage(languageType: string) {
  let languageModules
  if (languageType === 'zh') {
    languageModules = import.meta.glob('./modules/**/zh.ts', {
      import: 'default',
      eager: true,
    })
  } else {
    languageModules = import.meta.glob('./modules/**/en.ts', {
      import: 'default',
      eager: true,
    })
  }

  const language: { [propName: string]: any } = {}
  for (const path in languageModules) {
    let mouduleName = ''
    if (languageType === 'zh') {
      mouduleName = path.match(/.\/modules\/(\S*)\/zh.ts/)?.[1]
    } else {
      mouduleName = path.match(/.\/modules\/(\S*)\/en.ts/)?.[1]
    }
    language[mouduleName] = languageModules[path]
  }
  return language
}
const i18n = createI18n({
  legacy: false, // 如果要支持 compositionAPI，此项必须设置为 false
  locale: 'zh', // 设置语言类型
  globalInjection: true, // 全局注册$t方法
  messages: {
    zh: loadLanguage('zh'),
    en: loadLanguage('en'),
  },
})

export default i18n
